package main

import (
	"bytes"
	"fmt"
	"net/http"
	"os/exec"
)

/*
https://blog.csdn.net/wangshubo1989/article/details/70294510

*/

func ExecShell(s string) int {
	MyPrintf("\n get comm:\n" + s + "\n ")
	_, os := GetOsInfo()
	var out bytes.Buffer
	if os == "windows" {
		cmd := exec.Command("cmd.exe", "/c", "start "+s)
		cmd.Stdout = &out
		err := cmd.Run()
		if err != nil {
			fmt.Printf("error win: %v \n", err)
			return 0
		}
	} else {
		cmd := exec.Command("/bin/bash", "-c", s)
		cmd.Stdout = &out
		err := cmd.Run()
		if err != nil {
			fmt.Printf("error: %v \n", err)
			return 0
		}
	}
	//fmt.Printf("------A:%v \n ", out)
	return 1
}

//func ExecShell_test() {
//ExecShell("uname ")
//rs := ExecShell("avrdude -C /Users/user01/Desktop/hex/avrdude.conf -v -p atmega2560 -c wiring -P /dev/tty.wchusbserial14230 -b 115200 -D -U flash:w:/Users/user01/Desktop/hex/k1.hex:i")
//rs := ExecShell("avrdude -C /Users/user01/Desktop/hex/avrdude.conf -v -p atmega328p -c arduino -P /dev/tty.wchusbserial14230 -b 115200 -D -Uflash:w:/Users/user01/Desktop/hex/k2.hex:i")
//fmt.Printf("------B:%i \n ", rs )
//}

func execShellCommer(comm string, w http.ResponseWriter, ostype int) {
	rs := ExecShell(comm)
	msg := Message{D: IntToString(rs), C: "se.ssh"}
	messageSendBackClient(msg, w, ostype)
}
